// Town 1: The Library
// t1Library.txt
// This is the town script for the Library in the HLPM. The main scripting here
// is for the Ability Rune. Those states are 10-13. States 14, 15, and 16 are
// for the stairs.

begintownscript;

variables;
// Standard vars
short i,j,k,choice;
short what_num = 0; // For the numeric input in the Ability Rune
string dlgstr;

body;

beginstate INIT_STATE;
	set_crime_tolerance(1);

	// Get rid of those ugly braziers. They're only there for light.
	set_terrain(18,33,0);
	set_terrain(20,24,0);
	set_terrain(29,22,0);
	set_terrain(41,1,0);
	set_terrain(18,17,0);
	set_terrain(18,14,0);
	set_terrain(18,11,0);
	set_terrain(18,8,0);
	set_terrain(18,5,0);
	set_terrain(18,2,0);
	
	// Make it impossible to use the combat bug to walk up the stairs.
	change_blocked(17,40,1);
	change_blocked(18,40,1);
	change_blocked(19,40,1);
	change_blocked(20,40,1);
	change_blocked(21,40,1);
	change_blocked(17,41,1);
	change_blocked(18,41,1);
	change_blocked(19,41,1);
	change_blocked(20,41,1);
	change_blocked(21,41,1);
	
	change_blocked(26,32,1);
	change_blocked(26,33,1);
	change_blocked(26,34,1);
	change_blocked(27,32,1);
	change_blocked(27,33,1);
	change_blocked(27,34,1);
	
	change_blocked(39,7,1);
	change_blocked(40,7,1);
	change_blocked(41,7,1);
	change_blocked(39,8,1);
	change_blocked(40,8,1);
	change_blocked(41,8,1);
	
	set_name(6,"Librarian");
break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;

// If the party is in combat mode, tell them to cut it out.
if (is_combat())
	message_dialog("Entering combat mode outside the Arena is NOT recommended.","");

// Punish crime with instant death.
if (get_crime_level() >= 1) {
	kill_char(1000,2,0);
	message_dialog("Crimes in the HLPM are punishable by instant death, as you discover the hard way.","");
	}

break;

beginstate 10; 
	// Change the party's character traits. This involved states 10-13.
	// In state 10, the player chooses to modify a character. In state 11, the
	// player chooses which character to modify. In state 12, the HLPM tells the
	// player what traits that character has, and the player chooses again to
	// modify those traits. In state 13, the player chooses which trait to
	// modify and actually modifies the traits.
	//
	// The excessive number of states is necessary because of the jumping around
	// that happens. The player can go through and do extensive modification on
	// as many traits as desired for as many characters after stepping on the
	// rune only once.
	
	// Never let them step on the rune. This is easier for the player.
	block_entry(1);
	
	// Do they wish to use the rune? If not, stop everything.
	reset_dialog();
	add_dialog_str(0,"At this rune, you can change your party's character traits. Would you like to modify one of your characters?",0);
	add_dialog_choice(0,"No. I want to leave.");
	add_dialog_choice(1,"Yes. What are my options?");
	if (run_dialog(1) == 1)
		end();
		
	set_state_continue(11);
break;

beginstate 11;	
	// Choose a character.
	message_dialog("You must now choose which character to modify.","");
	while (run_select_a_pc(1) == 0)
		// If the player cancels, ask why.
		{reset_dialog();
		add_dialog_str(0,"You chose to cancel the character selection. Do you wish to try again?",0);
		add_dialog_choice(0,"No. I changed my mind.");
		add_dialog_choice(1,"Yes.");
		if (run_dialog(1) == 1)
			end();
		}
	
	set_state_continue(12);
break;

beginstate 12;	
	// Tell the player what the character's traits are right now.
	j = 0;
	clear_buffer();
	append_string("This character has the traits: ");
	if (char_has_trait(get_selected_pc(),0) > 0)
		{j = 1;
		append_string("Strong Back");
		}
	if (char_has_trait(get_selected_pc(),1) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Nimble Fingers");
		}
	if (char_has_trait(get_selected_pc(),2) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Beastmaster");
		}
	if (char_has_trait(get_selected_pc(),3) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Strong Will");
		}
	if (char_has_trait(get_selected_pc(),4) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Good Education");
		}
	if (char_has_trait(get_selected_pc(),5) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Toughness");
		}
	if (char_has_trait(get_selected_pc(),6) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Fast on Feet");
		}
	if (char_has_trait(get_selected_pc(),7) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Natural Mage");
		}
	if (char_has_trait(get_selected_pc(),8) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Elite Warrior");
		}
	if (char_has_trait(get_selected_pc(),9) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Divinely Touched");
		}
	if (char_has_trait(get_selected_pc(),10) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Cursed At Birth");
		}
	if (char_has_trait(get_selected_pc(),11) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Sickness Prone");
		}
	if (char_has_trait(get_selected_pc(),12) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Sluggish");
		}
	if (char_has_trait(get_selected_pc(),13) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Brittle Bones");
		}
	if (char_has_trait(get_selected_pc(),14) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Completely Inept");
		}
	if (j == 0)
		append_string("no traits");
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(0,dlgstr,0);
	
	// Also tell player what abilities the character has.
	j = 0;
	clear_buffer();
	append_string("This character has the abilities: ");
	if (char_has_trait(get_selected_pc(),15) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Summon Beast");
		}
	if (char_has_trait(get_selected_pc(),16) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Call Spirit");
		}
	if (char_has_trait(get_selected_pc(),17) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Regenerate");
		}
	if (char_has_trait(get_selected_pc(),18) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Cure Afflictions");
		}
	if (char_has_trait(get_selected_pc(),19) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Battle Frenzy");
		}
	if (char_has_trait(get_selected_pc(),20) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Divine Aid");
		}
	if (char_has_trait(get_selected_pc(),21) > 0)
		{if (j)
			append_string(", ");
		j = 1;
		append_string("Restore Energy");
		}
	if (j == 0)
		append_string("no abilities");
	append_string(".");
	get_buffer_text(dlgstr);
	add_dialog_str(1,dlgstr,0);
	
	add_dialog_str(2,"Possible traits: Strong Back, Nimble Fingers, Beastmaster, Strong Will, Good Education, Toughness, Fast on Feet, Natural Mage, Elite Warrior, Divinely Touched, Cursed At Birth, Sickness Prone, Sluggish, Brittle Bones, and Completely Inept.",0);
	add_dialog_str(3,"Possible abilities: Summon Beast, Call Spirit, Regenerate, Cure Afflictions, Battle Frenzy, Divine Aid, and Restore Energy.",0);
	add_dialog_str(4,"If you would like to modify a trait, choose that option and enter the name or number of the trait that you would like to modify. Would you like to modify a trait?",0);
	add_dialog_choice(0,"No. I don't want to modify anything.");
	add_dialog_choice(1,"Yes.");
	add_dialog_choice(2,"I don't want to modify this character, but I'd like to modify a different one.");
	choice = run_dialog(1);
	if (choice == 1)
		end();
	
	// If the player chose to modify this char, go forward.
	if (choice == 2)
		set_state_continue(13);
		
	// If the player wants to modify a different one, go back a step.
	set_state_continue(11); 
break;

beginstate 13;
	// Select a trait to modify.
	what_num = 50;
	
	get_text_response("Which trait?");
	check_text_response_match("0");
	if (got_text_match())
		what_num = 0;
	check_text_response_match("Strong Back");
	if (got_text_match())
		what_num = 0;
	check_text_response_match("1");
	if (got_text_match())
		what_num = 1;
	check_text_response_match("Nimble Fingers");
	if (got_text_match())
		what_num = 1;
	check_text_response_match("2");
	if (got_text_match())
		what_num = 2;
	check_text_response_match("Beastmaster");
	if (got_text_match())
		what_num = 2;
	check_text_response_match("3");
	if (got_text_match())
		what_num = 3;
	check_text_response_match("Strong Will");
	if (got_text_match())
		what_num = 3;
	check_text_response_match("4");
	if (got_text_match())
		what_num = 4;
	check_text_response_match("Good Education");
	if (got_text_match())
		what_num = 4;
	check_text_response_match("5");
	if (got_text_match())
		what_num = 5;
	check_text_response_match("Toughness");
	if (got_text_match())
		what_num = 5;
	check_text_response_match("6");
	if (got_text_match())
		what_num = 6;
	check_text_response_match("Fast on Feet");
	if (got_text_match())
		what_num = 6;
	check_text_response_match("7");
	if (got_text_match())
		what_num = 7;
	check_text_response_match("Natural Mage");
	if (got_text_match())
		what_num = 7;
	check_text_response_match("8");
	if (got_text_match())
		what_num = 8;
	check_text_response_match("Elite Warrior");
	if (got_text_match())
		what_num = 8;
	check_text_response_match("9");
	if (got_text_match())
		what_num = 9;
	check_text_response_match("Divinely Touched");
	if (got_text_match())
		what_num = 9;
	check_text_response_match("10");
	if (got_text_match())
		what_num = 10;
	check_text_response_match("Cursed At Birth");
	if (got_text_match())
		what_num = 10;
	check_text_response_match("11");
	if (got_text_match())
		what_num = 11;
	check_text_response_match("Sickness Prone");
	if (got_text_match())
		what_num = 11;
	check_text_response_match("12");
	if (got_text_match())
		what_num = 12;
	check_text_response_match("Sluggish");
	if (got_text_match())
		what_num = 12;
	check_text_response_match("13");
	if (got_text_match())
		what_num = 13;
	check_text_response_match("Brittle Bones");
	if (got_text_match())
		what_num = 13;
	check_text_response_match("14");
	if (got_text_match())
		what_num = 14;
	check_text_response_match("Completely Inept");
	if (got_text_match())
		what_num = 14;
	check_text_response_match("15");
	if (got_text_match())
		what_num = 15;
	check_text_response_match("Summon Beast");
	if (got_text_match())
		what_num = 15;
	check_text_response_match("16");
	if (got_text_match())
		what_num = 16;
	check_text_response_match("Call Spirit");
	if (got_text_match())
		what_num = 16;
	check_text_response_match("17");
	if (got_text_match())
		what_num = 17;
	check_text_response_match("Regenerate");
	if (got_text_match())
		what_num = 17;
	check_text_response_match("18");
	if (got_text_match())
		what_num = 18;
	check_text_response_match("Cure Afflictions");
	if (got_text_match())
		what_num = 18;
	check_text_response_match("19");
	if (got_text_match())
		what_num = 19;
	check_text_response_match("Battle Frenzy");
	if (got_text_match())
		what_num = 19;
	check_text_response_match("20");
	if (got_text_match())
		what_num = 20;
	check_text_response_match("Divine Aid");
	if (got_text_match())
		what_num = 20;
	check_text_response_match("21");
	if (got_text_match())
		what_num = 21;
	check_text_response_match("Restore Energy");
	if (got_text_match())
		what_num = 21;
		
	// If the player entered something weird, ask what to do.
	if (what_num == 50)
		{reset_dialog();
		add_dialog_str(0,"That was neither a number from 0-21 nor the name of a trait or ability. Would you like to try that again?",0);
		add_dialog_choice(0,"No. I don't want to modify anything.");
		add_dialog_choice(1,"Yes, let me try again.");
		add_dialog_choice(2,"I decided I'd rather modify a different character.");
		choice = run_dialog(1);
		if (choice == 1)
			end();
		if (choice == 2) // If try again, restart this state.
			set_state_continue(12);
		set_state_continue(11); // If a different char, go back a few steps.
		}
	
	// The player chose a trait to change, so keep going.
	// Ask whether to add/subtract that trait.
	reset_dialog();
	clear_buffer();
	if (char_has_trait(get_selected_pc(),what_num) == 0)
		{append_string("Would you like to add trait number ");
		append_number(what_num);
		append_string(" to ");
		}
	else
		{append_string("Your character already has that trait. Would you like to remove trait number ");
		append_number(what_num);
		append_string(" from ");
		}
	append_char_name(get_selected_pc());
	append_string("?");
	get_buffer_text(dlgstr);
	add_dialog_str(0,dlgstr,0);
	add_dialog_choice(0,"No. I'd rather not modify anything.");
	add_dialog_choice(1,"Yes. Do it.");
	add_dialog_choice(2,"I changed my mind. Let me review my character's traits again.");
	choice = run_dialog(1);
	
	if (choice == 1)
		end();
	if (choice == 3)
		set_state_continue(12); // If player changed mind, back up.
	
	// Otherwise, actually change the trait.
	if (char_has_trait(get_selected_pc(),what_num) == 0)
		set_char_trait(get_selected_pc(),what_num,1);
	else
		set_char_trait(get_selected_pc(),what_num,0);
		
	// Redirect to the appropriate place.
	reset_dialog();
	add_dialog_str(0,"Your trait has been modified! What would you like to do now?",0);
	add_dialog_choice(0,"I'm done here.");
	add_dialog_choice(1,"I'd like to review this character's traits.");
	add_dialog_choice(2,"I'd like to modify a different character.");
	choice = run_dialog(1);
	
	if (choice == 1)
		end();
	if (choice == 2)
		set_state_continue(12); // To modify the same character.
	set_state_continue(11); // To modify a different character.
break;

beginstate 14; // Stairs down to Entrance
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Entrance. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Descend the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(0,28,10);
break;

beginstate 15; // Stairs up to Level Raiser
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Level Raiser. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(2,21,5);
break;

beginstate 16; // Stairs up to Level Raiser
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Level Raiser. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(2,24,4);
break;